$(document).ready(function(){
	// 直接用load()加载html
	$('#letter-a a').click(function(){
		$('#dictionary').load('a.html');
		alert('Loaded!');
		return false;
	});
	
	// 用$.getJSON()来加载一个json数据文件
	$('#letter-b a').click(function(){
		$.getJSON('b.json', function(data){
			var html = '';
			$.each(data, function(entryIndex, entry){
				html += '<div class="entry">';
				html += '<h3 class="term">' + entry.term + '</h3>';
				html += '<div class="part">' + entry.part + '</div>';
				html += '<div class="definition">';
				html += entry.definition;
				if(entry.quote){
					html += '<div class="quote">';
					$.each(entry.quote, function(lineIndex, line){
						html += '<div class="quote-line">' + line + '</div>';
					});
					if(entry.author){
						html += '<div class="quote-author">' + entry.author + '</div>';
					}
					html += '</div>';
				}			
				html += '</div>';
				html += '</div>';
			});
			$('#dictionary').html(html);
		});
		return false;
	});
	
	// 用$.getScript()来加载一个包含json数据及回调函数的的js文件
	$('#letter-c a').click(function(){
		$.getScript('c.js');
		return false;
	});
	
	// 用$.get()来加载xml
	$('#letter-d a').click(function(){
		$.get('d.xml', function(data){
			$('#dictionary').empty();
			$(data).find('entry:has(quote[author])').each(function(){
				var $entry = $(this);
				var html = '<div class="entry">';
				    html += '<h3 class="term">'+ $entry.attr('term') +'</h3>';
				    html += '<div class="part">'+ $entry.attr('part') +'</div>';
				    html += '<div class="definition">';
				    html += $entry.find('definition').text();
				var $quote = $entry.find('quote');
				if($quote.length){
					html += '<div class="quote">';
					$quote.find('line').each(function(){
						html += '<div class="quote-line">'+ $(this).text() +'</div>';
					});
					if($quote.attr('author')){
						html += '<div class="quote-author">'+ $quote.attr('author') +'</div>';
					}
					html += '</div>';
				}
				html += '</div';
				html += '</div>';
				$('#dictionary').append($(html));
			});
		});
		return false;
	});
	
	// 用$.get()请求php来返回结果
	$('#letter-e a').click(function(){
		var requestData = {term: $(this).text()};
		$.post('e.php', requestData, function(data){
			$('#dictionary').html(data);
		});
		return false;
	});
	
	/* form提交单个input,$.get()获取
	$('#letter-f form').submit(function(event){
		event.preventDefault();
		$.get('f.php', {term: $('input[name="term"]').val()},
			function(data){
				$('#dictionary').html(data);
			}
		);
	});
	*/
	
	// form多个input提交及获取
	$('#letter-f form').submit(function(event){
		event.preventDefault();
		var formValues = $(this).serialize();
		alert(formValues);
		$.get('f.php', formValues, function(data){
			$('#dictionary').html(data);
		});
	});
});


